home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 September (IDG) / Sep99.iso / Shareware World / Utilities / Text Processing / Alpha / Tcl / Modes / Perl Mode / perlMenu.tcl < prev    next >
Encoding:
Text File  |  1999-03-04  |  10.5 KB  |  382 lines  |  [TEXT/ALFA]

  1. # ◊◊◊◊ file loading dummy ◊◊◊◊ #
  2. proc perlMenu.tcl {} {}
  3.  
  4. #############################################################################
  5. # ◊◊◊◊ Build the perl menu ◊◊◊◊ #
  6. #            
  7.  
  8. menu::buildProc perlMenu menu::buildPerl
  9. menu::buildProc generalOptions menu::buildgeneralOptions
  10. menu::buildProc filterOptions menu::buildfilterOptions
  11. menu::buildProc perlFilterMenu rebuildFilterMenu
  12.  
  13. set perlFilterMenu "textFilters"
  14.  
  15. proc menu::buildPerl {} {
  16.     global PerlmodeVars perlFilterMenu perlMenu 
  17.     set ma {
  18.         "/'<Umacperl"
  19.         {Menu -m -n "tellMacperl..." -p perlTellProc {
  20.            "/O<UOpen This File"
  21.            "Save As Droplet"
  22.            "Save As Runtime"
  23.            "Save As CGI"
  24.             "(-"
  25.            "Get Output Window"
  26.            "Close Output Window"
  27.            "Quit"
  28.            }
  29.         } 
  30.         {Menu -m -n "Quick Save As..." -p perlSaveProc {
  31.            "Droplet"
  32.            "Runtime"
  33.            "CGI"
  34.            }
  35.         } 
  36.         {Menu -m -n help -p perlHelpProc {
  37.             "MacPerl Mode"
  38.             "Mac Specifics"
  39.             "Perl4 Manual"
  40.             "Perl5 Manual"
  41.         }}
  42.         "(-"
  43.         "runTheSelection"
  44.         "/R<UrunTheBuffer"
  45.         "/R<B<OsaveAndRun"
  46.         "runAFile"
  47.         "(-"
  48.     }
  49.     lappend ma [list Menu -n $perlFilterMenu {}] \
  50.       "selectBufferAsFilter" "selectFileAsFilter"
  51.     lappend ma "/F<UrepeatLastFilter"
  52. #    We enable this menu item without checking for a valid last filter; we'll 
  53. #    disable it after 'menu::buildSome perlMenu' is called. (RBC 02-MAR-1999)    
  54.     lappend ma "(-" \
  55.       [list Menu -n generalOptions {}] \
  56.       [list Menu -n filterOptions {}]
  57.  
  58.     return [list build $ma -1 \
  59.       {generalOptions filterOptions perlFilterMenu} $perlMenu]
  60. }
  61.  
  62. # General Perl-menu options menu
  63. #
  64. proc menu::buildgeneralOptions {} {
  65.     global PerlmodeVars
  66.     foreach i {"retrieveOutput" "autoSwitch" "promptForArgs" "useDebugger"} {
  67.         if [set PerlmodeVars(perl$i)] {
  68.             lappend ma "!•$i"
  69.         } else {
  70.             lappend ma $i
  71.         }
  72.     }
  73.     return [list build $ma]
  74. }
  75.  
  76. # Text Filter options menu
  77. #
  78. proc menu::buildfilterOptions {} {
  79.     global PerlmodeVars
  80.     Menu -n filterOptions {
  81.         "applyToBuffer"
  82.         "overwriteSelection"
  83.         "(-"
  84.         "rebuildFilterMenu"
  85.     }    
  86.     markMenuItem filterOptions overwriteSelection $PerlmodeVars(perloverwriteSelection)
  87.     markMenuItem filterOptions applyToBuffer $PerlmodeVars(perlapplyToBuffer)
  88. }
  89.  
  90. #############################################################################
  91. # ◊◊◊◊ filter menu builder ◊◊◊◊ #
  92. #  Build a submenu of "preattached" Perl filters using the names of the 
  93. #  scripts in the Text Filters directory.  Called whenever Text Filters
  94. # folder is reassigned.
  95. #
  96. proc rebuildFilterMenu {{args}} {
  97.     global PerlmodeVars perlFilters perlFilterMenu perlFilterPath
  98.     global PerlmodeVars $perlFilterMenu
  99.     
  100.     eval [menu::buildHierarchy [list $PerlmodeVars(perlFilterPath)] $perlFilterMenu textFiltersProc perlFilters]
  101. }
  102.  
  103. menu::buildSome perlMenu
  104. if {$PerlmodeVars(perlPrevScript) == {} || $PerlmodeVars(perlPrevScript) == {*startup*}} {
  105.         enableMenuItem $perlMenu repeatLastFilter 0 
  106.     } 
  107. # After building menu, disable "repeatLastFilter" command since there isn't a filter
  108. # yet. The if statement should always be true on the initial build. (RBC 02-MAR-1999) 
  109.  
  110.  
  111. # ◊◊◊◊ var & menu updater ◊◊◊◊ #
  112. # ShadowPerl sets the global PerlmodeVars vars when the mode vars are modified and
  113. # keeps the menu checkmarked correctly.
  114. #
  115. proc shadowPerl {name} {
  116.     global PerlmodeVars HOME perlMenu
  117.     switch $name {
  118.         "perluseDebugger"    {
  119.             markMenuItem generalOptions useDebugger $PerlmodeVars(perluseDebugger)
  120.          }
  121.         "perloverwriteSelection"    {
  122.             markMenuItem filterOptions overwriteSelection $PerlmodeVars(perloverwriteSelection)
  123.          }
  124.         "perlapplyToBuffer"    {
  125.             markMenuItem filterOptions applyToBuffer $PerlmodeVars(perlapplyToBuffer)
  126.          }
  127.         "perlretrieveOutput"    {
  128.             markMenuItem generalOptions retrieveOutput $PerlmodeVars(perlretrieveOutput) 
  129.         }
  130.         "perlautoSwitch" {    
  131.             markMenuItem generalOptions autoSwitch $PerlmodeVars(perlautoSwitch) 
  132.         }
  133.         "perlpromptForArgs" {    
  134.             markMenuItem generalOptions promptForArgs $PerlmodeVars(perlpromptForArgs) 
  135.         }
  136.         "perlVersion" {    
  137.             set modeCode "perl${perlVersion}.tcl"
  138.             if {[catch "$modeCode"]} {
  139.                 alertnote "Couldn't load the Perl-mode colorization file \"$modeCode\".  Contact the maintainer."
  140.             }
  141.         }
  142.         "perlLastFilter" {    
  143.             # Don't allow perlPrevScript to be changed from the flags menu
  144.             if {$PerlmodeVars(perlPrevScript) == "*startup*"} {
  145.                 set PerlmodeVars(perlPrevScript) $perlLastFilter
  146.                 enableMenuItem $perlMenu repeatLastFilter 1
  147.             } else {
  148.                 set perlLastFilter $PerlmodeVars(perlPrevScript) 
  149.             }
  150.         }
  151.     }
  152. }
  153.  
  154. #############################################################################
  155. # ◊◊◊◊ •Menu commands ◊◊◊◊ #
  156. #############################################################################
  157.  
  158. ###########################################################################
  159. #
  160. proc perlHelpProc {menu item} {
  161.     global PerlmodeVars HOME
  162.     switch $item {
  163.         "MacPerl Mode"    {
  164.                 if {[catch {openFileQuietly "$HOME:Help:MacPerl Help"}]} {
  165.                     alertnote "File not found:\r$HOME:Help:MacPerl Help"
  166.                 }
  167.             }
  168.         "Mac Specifics"    {
  169.                 if {[catch {openFileQuietly "$HOME:Help:MacPerl.Specifics"}]} {
  170.                     alertnote "File not found:\r$HOME:Help:MacPerl.Specifics"
  171.                 }
  172.             }
  173.         "Perl4 Manual"    {
  174.                 if {[catch {openFileQuietly "$HOME:Help:Perl Commands"}]} {
  175.                     alertnote "File not found:\r$HOME:Help:Perl Commands"
  176.                 }
  177.             }
  178.         "Perl5 Manual"    {
  179.                 catch {editMark "$HOME:Help:Perl Commands" Perl5 -r}
  180.             }
  181.     }
  182. }
  183.  
  184. ############################################################################
  185. # ◊◊◊◊ toggle flags ◊◊◊◊ #
  186. # Toggle the perl menu flags
  187. #
  188. proc retrieveOutput {} {
  189.     perlFlip perlretrieveOutput
  190. }
  191.  
  192. proc useDebugger {} {
  193.     perlFlip perluseDebugger
  194. }
  195.  
  196. proc autoSwitch {} {
  197.     perlFlip perlautoSwitch
  198. }
  199.  
  200. proc perlFlip {var} {
  201.     global PerlmodeVars
  202.     set PerlmodeVars($var) [expr [set PerlmodeVars($var)] ? 0 : 1]
  203.     synchroniseModeVar $var $PerlmodeVars($var)
  204.     shadowPerl $var
  205. }
  206.  
  207. proc overwriteSelection {} {
  208.     perlFlip perloverwriteSelection
  209. }
  210.  
  211. proc applyToBuffer {} {
  212.     perlFlip perlapplyToBuffer
  213. }
  214.  
  215. proc promptForArgs {} {
  216.     perlFlip perlpromptForArgs
  217. }
  218.  
  219. #############################################################################
  220. # ◊◊◊◊ Switch to MacPerl ◊◊◊◊ #
  221. proc macperl {} {
  222.     app::launchFore McPL
  223. }
  224.  
  225. #############################################################################
  226. # ◊◊◊◊ other MacPerl interactions ◊◊◊◊ #
  227. # Interact with MacPerl in some other way besides executing a script
  228. #
  229. #DTH: note addition of two lines for auto-save
  230. proc perlTellProc {menu name} {
  231.     switch -exact $name {
  232.     "Open This File"        { openInMacperl }
  233.     
  234.     "Save As Droplet"        { saveThruMacperl "droplet" }
  235.     
  236.     "Save As Runtime"        { saveThruMacperl "runtime" }
  237.     
  238.     "Save As CGI"            { saveThruMacperl "cgi" }
  239.     
  240.     "Get Output Window"        { openPerlOutput }
  241.     
  242.     "Close Output Window"    { sendCloseWinName MacPerl $perlName ;
  243.                               sendCloseWinName MacPerl "Perl Debug" }
  244.                             
  245.     "Quit"                    { quitMacperl }
  246.     }
  247. }
  248.  
  249. proc perlSaveProc {menu name} {
  250.     switch -exact $name {
  251.     "Droplet"    { saveThruMacperl "auto-droplet" }
  252.     
  253.     "Runtime"    { saveThruMacperl "auto-runtime" }
  254.  
  255.     "CGI"        { saveThruMacperl "auto-cgi" }
  256.     }
  257. }
  258.  
  259. #############################################################################
  260. # ◊◊◊◊ open curr file in MacPerl ◊◊◊◊ #
  261. # Open the current file under MacPerl.  This used to useful for saving files 
  262. # as droplets or runtime scripts.  Maybe it's still useful for something...?
  263. #
  264. proc openInMacperl {} {
  265.     if {[winDirty]} {
  266.         case [askyesno -c "Save '[lindex [winNames] 0]'?"] in {
  267.             "yes" {save}
  268.             "no" {}
  269.             "cancel" {return}
  270.         }
  271.     }
  272.     set name [app::launchFore McPL]
  273.     sendOpenEvent -n [file tail $name] [win::Current]
  274. }
  275.  
  276. #############################################################################
  277. # ◊◊◊◊ save as droplet or runtime ◊◊◊◊ #
  278. # Save the script in the current window as a MacPerl droplet or 
  279. # runtime script.  
  280. #
  281. proc saveThruMacperl {type} {
  282.     global PerlmodeVars ALPHA
  283.  
  284.     set name [file tail [app::launchBack McPL]]
  285.     getWinInfo arr
  286.     if {$arr(dirty) == 1} {
  287.         case [askyesno -c "Save '[lindex [winNames] 0]' source file also?"] in {
  288.             "yes" {save}
  289.             "no" {}
  290.             "cancel" {return}
  291.         }
  292.     }
  293.     #DTH note the following "if" block which replaced what is in the new "else" block
  294.     set myName [lindex [winNames -f] 0]
  295.     if {$type == "auto-droplet" || $type == "auto-runtime"} {
  296.         if {[file extension $myName] == ".pl"} {
  297.             set destfile [AEFilename [file rootname $myName]]
  298.         } else {
  299.             set destfile [AEFilename [file rootname $myName]]
  300.         }
  301.     } elseif {$type == "auto-cgi"} {
  302.         set destfile [AEFilename "[file rootname $myName].cgi"]
  303.     } else {
  304.         set destfile [AEFilename [putfile {Save droplet as} [lindex [winNames] 0]]]
  305.     }
  306.  
  307.     set script [curlyq [getText [minPos] [maxPos]]]
  308.     #DTH note addition of "auto-xxx" in two lines below
  309.     if {$type == "droplet" || $type == "auto-droplet"} {
  310.         set saveType "SCPT"
  311.     } elseif {$type == "runtime" || $type == "auto-runtime"} {
  312.         set saveType "MrP7"
  313.     } elseif {$type == "cgi" || $type == "auto-cgi"} {
  314.         set saveType "'WWWΩ'"
  315.     } elseif {$type == "text"} {
  316.         set saveType "TEXT"
  317.     }
  318.     
  319.     set err [catch {eval "AEBuild -t 36000 -r \"$name\"" core save {----} [list $script] {dest:} [list $destfile] {fltp:} $saveType } reply ]
  320.     if {$err} { message "AEBuild error code $err in saveThruMacperl" }
  321.     
  322. # The following lines could be used to tell MacPerl to take the script file 
  323. # from an existing disk file and then re-save it in the desired form.
  324. #
  325. #    set srcfile "\[ [AEFilename [win::Current]] \]"
  326. #    set reply [eval "AEBuild -t 36000 -r \"$name\"" core save {----} [list $srcfile] {dest:} [list $destfile] {fltp:} $saveType ]
  327. #
  328. }
  329.  
  330. #############################################################################
  331. # ◊◊◊◊ Quit a running MacPerl app ◊◊◊◊ #
  332. proc quitMacperl {} {
  333.     foreach proc [processes] {
  334.         set sig [lindex $proc 1]
  335.         if {$sig == "McPL"} {
  336.             sendQuitEvent [lindex $proc 0]
  337.             # switchTo is necessary to keep MacPerl from blinking
  338.             switchTo [lindex $proc 0]    
  339.         }
  340.     }
  341. }
  342.  
  343. #############################################################################
  344. # ◊◊◊◊ run something via MacPerl ◊◊◊◊ #
  345. # (No special arrangements are made to provide input or capture the output)
  346. proc runTheSelection {} {
  347.     global PerlmodeVars scriptFile scriptStart
  348.     set scriptFile [win::Current]
  349.     set scriptStart [lindex [posToRowCol [getPos]] 0]
  350.     perlExecuteScript [getSelect]
  351. }
  352.  
  353. proc runTheBuffer {} {
  354.     global PerlmodeVars scriptFile scriptStart
  355.     set scriptFile [win::Current]
  356.     set scriptStart 1
  357.     perlExecuteScript [getText [minPos] [maxPos]]
  358. }
  359.  
  360. proc runAFile {} {
  361.     global PerlmodeVars scriptFile scriptStart
  362.     if {! [catch {getfile "Select a Perl script"} path]} {
  363.         set scriptFile $path
  364.         set scriptStart 1
  365.         perlExecuteFile $path
  366.     }
  367. }
  368.  
  369. proc saveAndRun {} {
  370.     global PerlmodeVars scriptFile scriptStart
  371.     save
  372.     set path [win::Current]   
  373.     set scriptFile $path
  374.     set scriptStart 1
  375.     perlExecuteFile $path
  376. }
  377.  
  378.  
  379.